package com.example.recyclerimageload.glide_load_img;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.recyclerimageload.R;
import com.example.recyclerimageload.glide_load_img.adapter.MyAdapter;
import com.example.recyclerimageload.glide_load_img.adapter.MyItemDecoration;
import com.example.recyclerimageload.glide_load_img.bean.UrlsBean;
import com.example.recyclerimageload.glide_load_img.utils.Utils;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerActivity extends AppCompatActivity implements MyAdapter.OnItemClickListener {

    private static final String TAG = "cai";
    private RecyclerView recyclerView;
    private MyAdapter myAdapter;
    private List<UrlsBean> photoUrls;
    private ImageView imageView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);

        init();
    }

    private void init() {
        imageView = ((ImageView) findViewById(R.id.ivsw));
        recyclerView = ((RecyclerView) findViewById(R.id.recycler));
        myAdapter = MyAdapter.getInstance();
        photoUrls = Utils.getPhotoUrls();

        GridLayoutManager manager = new GridLayoutManager(this,4);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return photoUrls.get(position).isFirst()?4:1;
            }
        });

        recyclerView.addItemDecoration(new MyItemDecoration(new MyItemDecoration.OnPositionListener() {
            @Override
            public boolean isFist(int position) {
                if (photoUrls.get(position).isFirst()){
                    return true;
                }
                return false;
            }
        }));

        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(myAdapter);
        myAdapter.setDatas(photoUrls);
        myAdapter.setOnClickListener(this);
    }

    @Override
    public void onItemClick(View view) {
        int position = recyclerView.getChildAdapterPosition(view);
        Log.d(TAG, "onItemClick: position="+position);
        UrlsBean urlsBean = photoUrls.get(recyclerView.getChildAdapterPosition(view));
        String url = urlsBean.getUrl();
        Glide.with(this).load(url).into(imageView);
    }

    @Override
    public void onItemLongClick(View view) {
        int position = recyclerView.getChildAdapterPosition(view);
        Log.d(TAG, "onItemLongClick: position="+position);
    }
}
